/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.se2.meteocal.entity;

import it.polimi.se2.meteocal.control.PasswordEncrypter;
import java.io.Serializable;
import java.util.Objects;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author Ugo
 */
@Entity
@XmlRootElement()
@XmlAccessorType(XmlAccessType.FIELD)
@Table(name = "users")
@NamedQueries({
    @NamedQuery(name = "Users.findAll", query = "SELECT u FROM Users u"),
    @NamedQuery(name = "Users.findByEmail", query = "SELECT u FROM Users u WHERE u.email = :email"),
})
  
public class Users implements Serializable {
    
    public static final String findByEmail = "Users.findByEmail";
    public static final String findAll = "Users.findAll"; 
    
    @XmlAttribute
    @Id
    @Pattern(regexp = "[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?",
            message = "Invalid email")
    @Basic(optional = false)
    @Size(min = 1, max = 255)
    @NotNull(message = "May not be empty")
    @Column(name = "EMAIL")
    private String email;
    
    @XmlAttribute
    @Size(max = 255)
    @Column(name = "GROUPNAME")
    @NotNull(message = "May not be empty")
    private String groupname;
    
    @XmlAttribute
    @Size(max = 255)
    @Column(name = "NAME")
    @NotNull(message = "May not be empty")
    private String name;
      
    @XmlAttribute
    @Size(max = 255)
    @Column(name = "SURNAME")
    @NotNull(message = "May not be empty")
    private String surname;
    
    @XmlAttribute
    @Size(max = 255)
    @Column(name = "PASSWORD")
    @NotNull(message = "May not be empty")
    private String password;
    
    @XmlAttribute
    @Size(max = 255)
    @Column(name = "CITY")
    @NotNull(message = "May not be empty")
    private String city;

    public Users() {
    }

    public Users(String email) {
        this.email = email;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getGroupname() {
        return groupname;
    }

    public void setGroupname(String groupname) {
        this.groupname = groupname;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
         this.password = PasswordEncrypter.encryptPassword(password);
    }
    
    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }
    
    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }
    
    
    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Users other = (Users) obj;
        return Objects.equals(this.email, other.email);
    }

    
    
}
